package com.dingding.nettyserver.modular.speicial.driver.impl;

import java.math.BigDecimal;

import javax.annotation.Resource;

import org.apache.http.HttpStatus;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import com.dingding.common.core.base.dto.FacadeResult;
import com.dingding.facade.special.ISpecialFacade;
import com.dingding.facade.special.trasfer.today_info.ReqGetTodayInfo;
import com.dingding.facade.special.trasfer.today_info.ResGetTodayInfo;
import com.dingding.facade.special.trasfer.up_posi.ReqSetUserStatusDTO;
import com.dingding.nettyserver.common.protocolbuf.SpecialDriverProbuf.CalcPriceRequest;
import com.dingding.nettyserver.common.protocolbuf.SpecialDriverProbuf.CalcPriceResponse;
import com.dingding.nettyserver.common.protocolbuf.SpecialDriverProbuf.UpPosiRequest;
import com.dingding.nettyserver.common.protocolbuf.SpecialDriverProbuf.UpPosiResponse;
import com.dingding.nettyserver.common.trasferrule.response.HandlerResult;
import com.dingding.nettyserver.common.trasferrule.response.ResultCode;
import com.dingding.nettyserver.core.session.Session;
import com.dingding.nettyserver.core.session.SessionManager;
import com.dingding.nettyserver.core.util.NumUtil;
import com.dingding.nettyserver.modular.speicial.driver.IDriverHandler;
import com.dingding.order.common.dto.distance.ReqRealTimeCalculationDTO;
import com.dingding.order.common.dto.distance.ResRealTimeCalculationDTO;
import com.dingding.order.facade.common.distance.ICalculationFacade;
import com.google.protobuf.InvalidProtocolBufferException;

/**
 * @Description 专车业务，司机模块的相关请求
 * @author fengshuonan
 * @date 2016年11月30日 下午5:12:17
 */
@Component
public class DriverHandlerImpl implements IDriverHandler {

	@Resource(name = "specialFacade")
	private ISpecialFacade specialFacade;

	@Resource(name = "calculationFacade")
	private ICalculationFacade calculationFacade;

	/**
	 * @Description 实时上传位置，并获取当日流水等信息
	 * @author fengshuonan
	 * @throws InvalidProtocolBufferException
	 */
	@Override
	public HandlerResult<?> actual(Session session, byte[] data) throws InvalidProtocolBufferException {

		// 解析客户端的protobuf数据
		UpPosiRequest upPosiRequest = UpPosiRequest.parseFrom(data);

		// 加入到session
		session.setAttachment(upPosiRequest.getUserId());
		SessionManager.putSession(upPosiRequest.getUserId(), session);

		// 把请求的数据封装到ReqGetTodayInfo
		ReqGetTodayInfo getTodayInfo = new ReqGetTodayInfo();
		BeanUtils.copyProperties(upPosiRequest, getTodayInfo);

		// 把请求的数据封装到ReqSetUserStatusDTO
		ReqSetUserStatusDTO setUserStatusDTO = new ReqSetUserStatusDTO();
		BeanUtils.copyProperties(upPosiRequest, setUserStatusDTO);
		setUserStatusDTO.setOperationType(3); // 3为上传位置

		// 调用dubbo服务获取司机当日信息，并且上传司机的经纬度信息
		FacadeResult<ResGetTodayInfo> driverTodayInfo = null;
		
		// 如果用户角色3,则为出租车,否则为专车
		if(getTodayInfo.getUserRole() == null || getTodayInfo.getUserRole() == 1 || getTodayInfo.getUserRole() == 2){
			driverTodayInfo = this.specialFacade.getDriverTodayInfo(getTodayInfo);
		}else if(getTodayInfo.getUserRole() == 3){
			driverTodayInfo = this.specialFacade.getTaxiDriverTodayInfo(getTodayInfo);
		}
		
		FacadeResult<Object> uploadPosition = this.specialFacade.uploadPosition(setUserStatusDTO);

		ResGetTodayInfo resGetTodayInfo = null;

		// 获取dubbo服务的执行结果
		if (driverTodayInfo.getCode() == HttpStatus.SC_OK && uploadPosition.getCode() == HttpStatus.SC_OK) {
			resGetTodayInfo = driverTodayInfo.getObject();

			// 把dubbo相关的数据转成字符串
			String completionTurnover = NumUtil.keep1Point(resGetTodayInfo.getCompletionTurnover());
			String onLineTime = NumUtil.keep1Point(resGetTodayInfo.getOnLineTime());

			UpPosiResponse posiResponse = UpPosiResponse.newBuilder().setSupStatus(resGetTodayInfo.getSupStatus())
					.setTakeOrderNumber(resGetTodayInfo.getTakeOrderNumber())
					.setRurnoverRate(resGetTodayInfo.getRurnoverRate()).setCompletionTurnover(completionTurnover)
					.setOnLineTime(onLineTime).build();
			return HandlerResult.SUCCESS(posiResponse);
		}

		return HandlerResult.ERROR(ResultCode.AGRUMENT_ERROR);
	}

	/**
	 * @Description 计算距离价格
	 * @author fengshuonan
	 * @throws InvalidProtocolBufferException
	 */
	// @Override
	// public HandlerResult<?> calcPrice(Session session, byte[] data) throws
	// InvalidProtocolBufferException {
	//
	// // 解析客户端的protobuf数据
	// CalcPriceRequest calcPriceRequest = CalcPriceRequest.parseFrom(data);
	//
	// ReqCalculationAmountDTO calculationAmountDTO = new
	// ReqCalculationAmountDTO();
	// BeanUtils.copyProperties(calcPriceRequest, calculationAmountDTO);
	//
	// // 调用dubbo的special服务计算距离价格
	// FacadeResult<ResCalculationAmountDTO> facadeResult =
	// this.specialFacade.calcOrderPrice(calculationAmountDTO);
	// if (facadeResult.getCode() == HttpStatus.SC_OK) {
	// ResCalculationAmountDTO resultObj = facadeResult.getObject();
	//
	// // 转化duble到string
	// String amount = NumUtil.keep1Point(resultObj.getAmount());
	// String ditance = NumUtil.keep1Point(resultObj.getDistance());
	// CalcPriceResponse calcPriceResponse =
	// CalcPriceResponse.newBuilder().setAmount(amount).setDistance(ditance)
	// .setDuration(resultObj.getDuration()).setPublishType(resultObj.getPublishType()).build();
	// return HandlerResult.SUCCESS(calcPriceResponse);
	// } else {
	// return HandlerResult.ERROR(ResultCode.UNKOWN_EXCEPTION);
	// }
	// }

	/**
	 * @Description 计算距离价格
	 * @author fengshuonan
	 * @throws InvalidProtocolBufferException
	 */
	@Override
	public HandlerResult<?> calcPrice(Session session, byte[] data) throws InvalidProtocolBufferException {

		// 解析客户端的protobuf数据
		CalcPriceRequest calcPriceRequest = CalcPriceRequest.parseFrom(data);

		ReqRealTimeCalculationDTO reqRealTimeCalculationDTO = new ReqRealTimeCalculationDTO();

		BeanUtils.copyProperties(calcPriceRequest, reqRealTimeCalculationDTO);

		// 获取实时计算距离价格响应Bean
		ResRealTimeCalculationDTO resRealTimeCalculationDTO = calculationFacade
				.realTimeCalculation(reqRealTimeCalculationDTO);

		if (resRealTimeCalculationDTO != null) {

			// 转化duble到string
			String amount = resRealTimeCalculationDTO.getAmount().setScale(1,BigDecimal.ROUND_UP).toString();
			String ditance = resRealTimeCalculationDTO.getDistance().setScale(1,BigDecimal.ROUND_UP).toString();
			CalcPriceResponse calcPriceResponse = CalcPriceResponse.newBuilder().setAmount(amount).setDistance(ditance)
					.setDuration(resRealTimeCalculationDTO.getDuration().intValue()).setPublishType(2).build();
			return HandlerResult.SUCCESS(calcPriceResponse);
		} else {
			return HandlerResult.ERROR(ResultCode.UNKOWN_EXCEPTION);
		}

	}

}
